//
//  BaseViewController.m
//  dyseller
//
//  Created by 红喇叭 on 2017/8/11.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "BaseViewController.h"
#import "SGGenerateQRCodeVC.h"
#import "SGScanningQRCodeVC.h"
#import <AVFoundation/AVFoundation.h>
#import "SGAlertView.h"
#import "UIButton+EnlargeTouchArea.h"


@interface BaseViewController ()

@end

@implementation BaseViewController
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorFromRGB(0xfafafa);
    __weak typeof(self) weakself = self;
    _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _backBtn.frame = CGRectMake(10, 0, 44, 44);

    [_backBtn setImage:[UIImage imageNamed:@"caiwu_nav_icon_default.png"] forState:UIControlStateNormal];
    //增大点击范围
    [_backBtn setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
    if (@available(iOS 11, *)) {
        _backBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
        [_backBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -1 *SCREEN_W /375.0,0,0)];
    }

    [_backBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        [weakself.navigationController popViewControllerAnimated:YES];
    }];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_backBtn];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(againLogin) name:@"Login" object:nil];
    
   // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(againLogin) name:@"abnormal-Login" object:nil];
    
    [self checkNetworkStatus];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
}
#pragma mark -- 实时监测网络状态的变化
- (void)checkNetworkStatus {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请检查网络连接是否正常" delegate:nil cancelButtonTitle:@"确定"otherButtonTitles:nil, nil];
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        /*
         status
         AFNetworkReachabilityStatusUnknown          = -1, 不知道监测的是什么
         AFNetworkReachabilityStatusNotReachable     = 0,  没有检测到网络
         AFNetworkReachabilityStatusReachableViaWWAN = 1,  蜂窝网
         AFNetworkReachabilityStatusReachableViaWiFi = 2,  WIFI
         */
        switch (status) {
                //没有检测到网络
            case AFNetworkReachabilityStatusNotReachable:
                [alert show];
                break;
            default:
                break;
        }
    }];
    // 开始监测
    [manager startMonitoring];
}

- (void)againLogin{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您的用户信息已过期！！" preferredStyle:UIAlertControllerStyleAlert];
    //添加取消到UIAlertController中
    [alertController addAction:[UIAlertAction actionWithTitle:@"去登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [userDefaults removeObjectForKey:@"admin_id"];
        [[AppDelegate delegate] LoginVC];
        
    }]];
    [self presentViewController:alertController animated:YES completion:nil];

}

- (void)setNavTitle:(NSString *)title  titleColor:(UIColor *)color font:(UIFont *)font{
    UILabel *lav = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 24)];
    lav.textColor = color;
    lav.backgroundColor = [UIColor clearColor];
    lav.font = font;
    lav.text = title;
    lav.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = lav;

}

//点击空白
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

// 扫码
-(void)scan
{
    // 1、 获取摄像设备
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if (device) {
        AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        if (status == AVAuthorizationStatusNotDetermined) {
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                if (granted) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        
                        SGScanningQRCodeVC *scanningQRCodeVC = [[SGScanningQRCodeVC alloc] init];
                        [self.navigationController pushViewController:scanningQRCodeVC animated:YES];
                        NSLog(@"主线程 - - %@", [NSThread currentThread]);
                    });
                    NSLog(@"当前线程 - - %@", [NSThread currentThread]);
                    
                    // 用户第一次同意了访问相机权限
                    NSLog(@"用户第一次同意了访问相机权限");
                    
                } else {
                    
                    // 用户第一次拒绝了访问相机权限
                    NSLog(@"用户第一次拒绝了访问相机权限");
                }
            }];
        } else if (status == AVAuthorizationStatusAuthorized) { // 用户允许当前应用访问相机
            SGScanningQRCodeVC *scanningQRCodeVC = [[SGScanningQRCodeVC alloc] init];
            [self.navigationController pushViewController:scanningQRCodeVC animated:YES];
        } else if (status == AVAuthorizationStatusDenied) { // 用户拒绝当前应用访问相机
            SGAlertView *alertView = [SGAlertView alertViewWithTitle:@"⚠️ 警告" delegate:nil contentTitle:@"请去-> [设置 - 隐私 - 相机 - SGQRCodeExample] 打开访问开关" alertViewBottomViewType:(SGAlertViewBottomViewTypeOne)];
            [alertView show];
        } else if (status == AVAuthorizationStatusRestricted) {
            NSLog(@"因为系统原因, 无法访问相册");
        }
    } else {
        SGAlertView *alertView = [SGAlertView alertViewWithTitle:@"⚠️ 警告" delegate:nil contentTitle:@"未检测到您的摄像头, 请在真机上测试" alertViewBottomViewType:(SGAlertViewBottomViewTypeOne)];
        [alertView show];
    }
    
    
    
}


#pragma  mark ========异常界面的处理========
- (void)addExceptionAlView{
    if(_exView==nil){
        _exView = [[ExceptionalView alloc] initWithFrame:self.view.bounds];
        
        _exView.delegate = self;
        
        [self.view addSubview:_exView];

    }
}

- (void)removeExceptionView{
    if(_exView){
        [_exView removeFromSuperview];
        _exView = nil;
        _exView.delegate = nil;
    }
}

#pragma  mark =======ExceptionalView 的代理=========
- (void)buttonAction{
    
}

@end
